package com.zx.kitchen.index.common.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.IndexPage;

import java.util.List;

/**
 * Created by Administrator on 2017/3/1.
 */

public class IndexAdapter extends RecyclerView.Adapter<IndexAdapter.ViewHolder> {
    private List<IndexPage> mList;
    private Context context;

    public IndexAdapter(List<IndexPage> list,Context context) {
        this.mList = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_index, parent, false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        IndexPage indexPage = mList.get(position);
        holder.rvImg.setImageResource(indexPage.getImgId());
        holder.rvContent.setText(indexPage.getContent());
//        if (position == 1 || position == 2 ||position == 5) {
//            holder.adapterView.setBackgroundColor(context.getResources().getColor(R.color.gray));
//            holder.rvContent.setTextColor(context.getResources().getColor(R.color.white));
//        }else {
//            holder.adapterView.setBackgroundColor(context.getResources().getColor(R.color.white));
//            holder.rvContent.setTextColor(context.getResources().getColor(R.color.black));
//        }
        holder.adapterView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                mOnItemClickListener.onItemClick(v,position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    protected class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView rvImg;
        private TextView rvContent;
        private LinearLayout adapterView;

        public ViewHolder(View view) {
            super(view);
            rvImg = (ImageView) view.findViewById(R.id.rv_img);
            rvContent = (TextView) view.findViewById(R.id.rv_content);
            adapterView = (LinearLayout) view.findViewById(R.id.adapter_view);

        }
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
